﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Announcement.DAL.DataSetApplyMgrTableAdapters;
using System.Data;

namespace Announcement.Admin
{
    public partial class ApplyMsg : AdminLoginPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {


        }

        protected void lvApplyMgr_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            HiddenField hfId = (HiddenField)e.Item.FindControl("hfId");
            if (e.CommandName == "Edit")
            {
                Response.Redirect("~/Admin/ApplyEdit.aspx?Id=" + hfId.Value + "&mode=edit");
            }
            if (e.CommandName == "Read")
            {
                Response.Redirect("~/Admin/ApplyEdit.aspx?Id=" + hfId.Value + "&mode=read");
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.dBind();
            
        }

        protected void lvApplyMgr_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
        {
            DataPager dp = (DataPager)lvApplyMgr.FindControl("DataPager1");
            dp.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
            this.dBind();
        }

        private void dBind()
        {
            string cCode = txtCCode.Text;
            string local = txtLocal.Text;
            string typeName = ddlApplyType.SelectedItem.Text;
            if (typeName == "请选择")
            {
                typeName = "";
            }
            DateTime startDate = DateTime.Parse("1753-01-01");
            DateTime endDate = DateTime.Parse("9999-12-31");

            if (txtStartDate.Text.Length > 0 && txtEndDate.Text.Length > 0)
            { 
                startDate = DateTime.Parse(txtStartDate.Text);
                endDate = DateTime.Parse(txtEndDate.Text);
            }

            var applyMgrRow = new ApplyMgrTableAdapter();
            DataTable dt = new DataTable();
            //输入框全部为空时，查询所有数据
            if (cCode.Length==0 && local.Length==0 && typeName.Length==0
                && txtStartDate.Text.Length==0 && txtEndDate.Text.Length==0)
            {
                dt = applyMgrRow.GetApplyMgr();
            }
            else 
            {
                dt = applyMgrRow.SearchMgr(cCode, local, typeName, startDate, endDate);
            }

            lvApplyMgr.Items.Clear();
            lvApplyMgr.DataSourceID = null;
            lvApplyMgr.DataSource = dt;
            lvApplyMgr.DataBind();
        }

        protected void ddlApplyType_DataBound(object sender, EventArgs e)
        {
            ListItem li = new ListItem("请选择");
            ddlApplyType.Items.Insert(0, li);
        }

        protected void lbtnExit_Click(object sender, EventArgs e)
        {
            Session["AdminUserName"] = null;
            Response.Redirect("../Login.aspx");
        }
    }
}
